﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunCMS.Common;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Controls.Page;


namespace YunCMS.Web.Admin.Module
{
    public partial class ProductManage : ModulePage<YCModuleProduct>//System.Web.UI.Page
    {
        private ModuleBLL<YCModuleProduct> _bll = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                TreeBind(this.categoryDDL, "news");
                HandleAction(saveBt);
            }
        }

        protected override ModuleBLL<YCModuleProduct> BLL
        {
            get
            {
                if (_bll == null)
                {
                    _bll = new YCModuleProductBLL();
                }
                return _bll;
            }
        }

        protected override void FillControls(YCModuleProduct model)
        {
            categoryDDL.SelectedValue = model.CategoryID.ToString();
            TitleTB.Text = model.Title.ToString();
            KeywordsTB.Text = model.Keywords.ToString();
            DescriptionTB.Text = model.Description.ToString();
            AuthorTB.Text = model.Author.ToString();
            IsHomePageCB.Checked = model.IsHomePage;

            //if (model.Access.HasValue)
            //    AccessTB.Text = model.Access.Value.ToString();

            IsPopularCB.Checked = model.IsPopular;
            ImageUrlTB.Text = model.ImageUrl.ToString();
            PriceTB.Text = model.Price.ToString();
            if (model.DiggTop.HasValue)
                DiggTopTB.Text = model.DiggTop.Value.ToString();

            if (model.DiggStep.HasValue)
                DiggStepTB.Text = model.DiggStep.Value.ToString();

            ///ActiveCB.Checked = model.Active;
            ContentTB.Text = model.Content.ToString();
        }

        protected override void FillModel(YCModuleProduct model)
        {
            model.Title = TitleTB.Text.Trim();
            model.Keywords = KeywordsTB.Text.Trim();
            model.Content = ContentTB.Text.Trim();
            model.Author = AuthorTB.Text.Trim();
            model.Description = DescriptionTB.Text.Trim();

            model.CategoryID = System.Convert.ToInt32(categoryDDL.SelectedValue);

            model.IsHomePage = System.Convert.ToBoolean(IsHomePageCB.Checked);

            //if (AccessTB.Text.Trim() != "")
            //    model.Access = System.Convert.ToInt32(AccessTB.Text.Trim());
            //else
            //    model.Access = 0;

            model.IsPopular = IsPopularCB.Checked;
            model.ImageUrl = ImageUrlTB.Text.Trim();

            if (PriceTB.Text.Trim() != "")
                model.Price = System.Convert.ToDecimal(PriceTB.Text.Trim());
            else
                model.Price = null;

            if (DiggTopTB.Text.Trim() != "")
                model.DiggTop = System.Convert.ToInt32(DiggTopTB.Text.Trim());
            else
                model.DiggTop = null;
            if (DiggStepTB.Text.Trim() != "")
                model.DiggStep = System.Convert.ToInt32(DiggStepTB.Text.Trim());
            else
                model.DiggStep = null;

            model.Active = false;
            //model.Active = System.Convert.ToBoolean(ActiveCB.Checked);
        }

        protected void saveBt_Click(object sender, EventArgs e)
        {            
            Save("ProductList.aspx");
        }
    }
}